<?php

class MDbTable extends Model {

	public function getList($filters = array())
	{
		if(!isset($filters['sort']))
		{
			if(isset($this->num) && $this->num)
			{
				$filters['sort'] = "num asc";
			}
			else
			{
				$filters['sort'] = "id asc";
			}
		}
		$this->_createSqlByFilters($filters);
		$res = $this->db->get();
		$rows = $res->result('array');
		$out = array();
		foreach ($rows as $row)
		{
			$this->_modifyRow($row);
			$out[$row['id']] = $row;
		}
		return $out;
	}

	public function getCount($filters = array())
	{
		$this->_createSqlByFilters($filters);
		return $this->db->count_all_results();
	}

	protected function _createSqlByFilters($filters)
	{
		foreach($filters as $filter_key => $filter_value)
		{
			switch($filter_key)
			{
				case "count":
					$this->db->limit($filter_value);
					break;
				case "offset":
					$this->db->offset($filter_value);
					break;
				case "sort":
					$arr = explode(",", $filter_value);
					foreach($arr as $v)
					{
						@list($order, $by) = explode(" ", $v);
						$this->db->order_by($order, $by);
					}
					break;

				case 'add_fields':
					break;

				default:
					if(is_array($filter_value))
					{
						if(count($filter_value))
						{
							$this->db->where_in($filter_key, $filter_value);
						}
					}
					else
					{
						$this->db->where($filter_key, $filter_value);
					}
					break;
			}
		}
		$this->db->from($this->table." t");
	}

	public function get($id)
	{
		$rows = $this->getList(array('id' => intval($id)));
		$row = count($rows) ? reset($rows) : array('id' => 0);
		return $row;
	}

	public function getFirstRow($filters = array())
	{
		$filters['count'] = 1;
		$rows = $this->getList($filters);
		if(count($rows))
		{
			$row = reset($rows);
		}
		else
		{
			$row = array();
		}
		return $row;
	}

	protected function _modifyRow(&$row)
	{
	}

	public function save($arr, &$id)
	{
		if($id)
		{
			$this->update($arr, $id);
		}
		else
		{
			$this->insert($arr, $id);
		}
	}

	public function insert($arr, &$id)
	{
		$this->db->insert($this->table, $arr);
		$id = $this->db->insert_id();
		if(isset($this->num) && $this->num)
		{
			$this->update(array('num' => $id), $id);
		}
	}

	public function update($arr, $id)
	{
		$this->db->update($this->table, $arr, array('id' => $id));
	}

	public function delete($id)
	{
		$this->db->delete($this->table, array('id' => $id));
	}

	function up($id)
	{
		$this->_move($id, true);
	}

	function down($id)
	{
		$this->_move($id, false);
	}

	function _move($id, $up)
	{
		$znak = $up ? "<" : ">";
		$by = $up ? 'desc' : 'asc';
		$res = $this->db->get_where($this->table, array('id' => $id));
		$row1 = $res->first_row('array');
		$this->db->where('num'.$znak.$row1['num']);
		if(isset($this->moveFields))
		{
			$moveFields = $this->moveFields;
			if(!is_array($moveFields))
			{
				$moveFields = array($moveFields);
			}
			foreach($moveFields as $field)
			{
				$this->db->where($field, $row1[$field]);
			}
		}
		$this->db->order_by('num', $by);
		$res = $this->db->get($this->table);
		if($res->num_rows())
		{
			$row2 = $res->first_row('array');
			$this->db->update($this->table, array('num' => $row1['num']), array('id' => $row2['id']));	
			$this->db->update($this->table, array('num' => $row2['num']), array('id' => $row1['id']));	
		}
	}


}

